# -*- encoding:utf-8 -*-
# __author__ = 'chenyongbing'
# __poject__ = 'ops_platform'
# __datetime__ = '2022/6/9'
# __name__ = 'handler.py'
from apps.system.models import Config
from apps.system.enums import ConfigItems
import copy
from django.conf import settings
from utils.signer_helper import Signer
signer = Signer(settings.SECRET_KEY)
# Create your code here.


def get_wxloginurl():
    group = 'wxlogin'
    data = get_config_by_group(group=group)
    wxloginurl = '{url}?appid={appid}&agentid={agentid}&redirect_uri={redirect_uri}&state={state}'.format(**data)
    return wxloginurl





def get_config_by_group(group):
    data = {}
    for config in Config.objects.filter(group=group):
        item = config.item
        value = config.value
        encrypt = config.encrypt
        if encrypt:
            value = signer.unsign(value)
        data[item] = value
    return data


def get_all_config_from_db():
    data = {}
    for config in Config.objects.all():
        group = config.group
        item = config.item
        if group not in data.keys():
            data[group] = {}
        data[group][item] = {
            'group': group,
            'item': config.item,
            'value': config.value,
            'encrypt': config.encrypt,
            'id': config.id
        }
    return data


def get_all_config_with_describe():
    data = get_all_config_from_db()
    NewConfigItems = copy.deepcopy(ConfigItems)
    for configItem in NewConfigItems:
        group = configItem.get('group')
        if not group:continue
        for item in configItem.get('items', []):
            name = item.get('item')
            item.update(data.get(group,{}).get(name, {}))
    return NewConfigItems